/*
 * Example01.java
 *
 * Created on 19 gennaio 2008, 17.17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package it.cnr.icib.flora2engine.examples;

import it.cnr.icib.flora2engine.Flora2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import net.sourceforge.flora.javaAPI.src.FloraMethod;
import net.sourceforge.flora.javaAPI.src.FloraObject;

/**
 *
 * @author Nino
 */
@Deprecated
public class ExampleALL {
    
    /** Creates a new instance of Example01 */
    public ExampleALL() {
    }
    
    public static void main(String[] args) {
    	Flora2 session ;
         /*
          -DPROLOGDIR=I:/Universita/TESI/Strumenti/xsb-3.1/config/x86-pc-windows/bin
                -DFLORA_FILE=flogicbasicsExample\\flogic_basics
          -DFLORADIR=I:/Universita/TESI/Strumenti/xsb-3.1/packages/flora2
          -DXSB_BIN_DIRECTORY=I:/Universita/TESI/Strumenti/xsb-3.1/config/x86-pc-windows/bin
          -Djava.library.path=I:/Universita/TESI/Strumenti/xsb-3.1/config/x86-pc-windows/bin
          -DENGINE=Subprocess
          */
        String curDir = System.getProperty("user.dir");
        
        System.out.println(curDir);
        String OS= System.getProperties().getProperty("os.name");
        System.out.println(OS);
        
        
        if (OS.contains("Mac")){
            session = new Flora2(curDir+"/xsb_mac_337/config/i386-apple-darwin11.4.2/bin",
            		curDir+     "/xsb_mac_337/packages/flora2");

        }else if(OS.contains("Win")){
        	session = new Flora2(curDir+"/xsb_win_31/config/x86-pc-windows/bin",
        			curDir+"/xsb_win_31/packages/flora2");
        }else{
        	session = new Flora2(curDir+"/xsb_lin/config/x86-pc-windows/bin",
        			curDir+"/xsb_lin/packages/flora2");
        }
        //String curDir = System.getProperty("user.dir");
        session.setPath(curDir + "/reasoner");
        
        String fileName = "test";
        
        if(fileName == null || fileName.trim().length() == 0) {
            System.out.println("Invalid path to example file!");
            System.exit(0);
        }
        
        //N:B il file si trova nella cartella .../packegs/flora2
        session.loadFile(fileName,"test");
        
        /* Examples of uses of the low-level Java-FLORA-2 API */
        
        // Querying persons
        String command = "?X:person@test.";
        //String command = "?X:secretary@example.";
        
        
        System.out.println("Query:"+command);
        Iterator personObjs = session.ExecuteQuery(command);
        /* Printing out persons  */
        while (personObjs.hasNext()) {
            Object personObj = personObjs.next();
            System.out.println("Person Id: "+personObj);
        }
        
        
        command = "person[instances -> ?X]@example.";
        System.out.println("Query:"+command);
        personObjs = session.ExecuteQuery(command);
        
        /* Printing out persons  */
        while (personObjs.hasNext()) {
            Object personObj = personObjs.next();
            System.out.println("Person Id: "+personObj);
        }
        
        /* Example of query with two variables */
        Vector<String> vars = new Vector<String>();
        vars.add("?X");
        vars.add("?Y");
        
        Iterator allmatches =
                session.ExecuteQuery("?X[believes_in -> ?Y]@example.",vars);
        System.out.println("Query:?X[believes_in -> ?Y]@example.");
        
        HashMap firstmatch;
        while (allmatches.hasNext()) {
            firstmatch = (HashMap)allmatches.next();
            FloraObject Xobj = (FloraObject)firstmatch.get("?X");
            FloraObject Yobj = (FloraObject)firstmatch.get("?Y");
            System.out.println(Xobj+" believes in: "+Yobj);
        }
        
        FloraObject personObj = new FloraObject("person",session.getFloraSession());
        Iterator methIter = personObj.getMethods("example");
        while (methIter.hasNext()) {
            System.out.println(((FloraMethod)methIter.next()).methodDetails());
        }
        
        // instances of the person class
        Iterator instanceIter = personObj.getInstances("example");
        System.out.println("Person instances:");
        while (instanceIter.hasNext())
            System.out.println("    " + (FloraObject)instanceIter.next());
        
        
        instanceIter = personObj.getDirectInstances("example");
        System.out.println("Person direct instances:");
        while (instanceIter.hasNext())
            System.out.println("    " + (FloraObject)instanceIter.next());
        
        Iterator subIter = personObj.getSubClasses("example");
        System.out.println("Person subclasses:");
        while (subIter.hasNext())
            System.out.println("    " + (FloraObject)subIter.next());
        
        subIter = personObj.getDirectSubClasses("example");
        System.out.println("Person direct subclasses:");
        while (subIter.hasNext())
            System.out.println("    " + (FloraObject)subIter.next());
        
        Iterator supIter = personObj.getSuperClasses("example");
        System.out.println("Person superclasses:");
        while (supIter.hasNext())
            System.out.println("    " + (FloraObject)supIter.next());
        
        supIter = personObj.getDirectSuperClasses("example");
        System.out.println("Person direct superclasses:");
        while (supIter.hasNext())
            System.out.println("    " + (FloraObject)supIter.next());
        
        /*****************************************************************
         ******** Examples of uses of the high-level Java-FLORA-2 API
         *****************************************************************/
        
        /* Printing out people's names and information about their kids
           using the high-level API. Note that the high-level person-object
           is obtained here out of the low-level FloraObject personObj
         */
        person currPerson = null;
        while (personObjs.hasNext()) {
            personObj = (FloraObject)(personObjs.next());
            System.out.println("Person name:"+personObj);
            
            currPerson =new person(personObj,"example");
            Iterator kidsItr = currPerson.getVDN_kids();
            
            while(kidsItr.hasNext()) {
                FloraObject kidObj = (FloraObject)(kidsItr.next());
                System.out.println("Person Name: " + personObj
                        + " has kid: " + kidObj);
                
                person kidPerson = null;
                kidPerson = new person(kidObj,"example");
                
                Iterator hobbiesItr = kidPerson.getVDN_hobbies();
                
                while(hobbiesItr.hasNext()) {
                    FloraObject hobbyObj = null;
                    hobbyObj = (FloraObject)(hobbiesItr.next());
                    System.out.println("Kid:"+kidObj
                            + " has hobby: " + hobbyObj);
                }
            }
        }
        
        
        FloraObject age;
        currPerson = new person("father(mary)", "example", session.getFloraSession());
        Iterator maryfatherItr = currPerson.getVDN_age();
        age = (FloraObject)maryfatherItr.next();
        System.out.println("Mary's father is " + age + " years old");
        
        currPerson = new person("mary", "example", session.getFloraSession());
        Iterator maryItr = currPerson.getVDN_age();
        age = (FloraObject)maryItr.next();
        System.out.println("Mary is " + age + " years old");
        
        // person instances using high-level interface
        person personClass = new person("person", "example", session.getFloraSession());
        instanceIter = personClass.getInstances();
        System.out.println("Person instances using high-level API:");
        while (instanceIter.hasNext())
            System.out.println("    " + (FloraObject)instanceIter.next());
        
        
        Iterator subclassIter = personClass.getSubClasses();
        System.out.println("Person subclasses using high-level API:");
        while (subclassIter.hasNext())
            System.out.println("    " + (FloraObject)subclassIter.next());
        
        
        // Close session and good bye
        session.close();
        System.exit(0);
    }
}
